<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Service;

/**
 * 车辆服务
 *
 * @author       Eric Lee
 */
class CarService {

    public function __construct($Car_id) {
        $this->view = new \Think\view();
        $this->Car_id = $Car_id;
        $this->Car = new \AuctionCore\CarResource\Car($this->Car_id);
        $this->report = $this->Car->report();
    }

    /**
     * 车辆信息
     * @return Ambigous <multitype:, \Think\mixed>
     */
    public function detail() {
        return $this->Car->detail();
    }

    /**
     * 检测报告
     * @return multitype:NULL string
     */
    public function report() {
        $report = array();
        $report['title'] = $this->title();
        $report['base_info'] = $this->base_info();
        $report['procedure'] = $this->procedure();
        $report['config'] = $this->config();
        $report['base_pics'] = $this->base_pics();
        $report['perform'] = $this->perform();
        $report['appear_history'] = $this->appear_history();
        $report['frame_history'] = $this->frame_history();

        //可见损伤
        $report['stat']['visible'] = $this->get_dm_stat(); //可见损伤统计
        $report['stat']['degree'] = $this->get_dm_stat('dm_degree'); //损伤程度统计

        //轻微损伤图片4张(小图)
        $a = $this->get_dm_pic('2');
        foreach($a as $k=>$v){
            $v['url'] = $v['url'].'@180w_120h_4e';
            $a[$k] =$v;
        }
        $report['light_pics'] = $a;
        unset($k);unset($v);

        //一般损伤图片4张(小图)
        $b = $this->get_dm_pic('3');
        foreach($b as $k=>$v){
            $v['url'] = $v['url'].'@180w_120h_4e';
            $b[$k] =$v;
        }
        $report['normal_pics'] = $b;
        unset($k);unset($v);

        //重要损伤图片4张
        $c = $this->get_dm_pic('4');
        foreach($c as $k=>$v){
            $v['url'] = $v['url'].'@180w_120h_4e';
            $c[$k] =$v;
        }
        $report['important_pics'] = $c;
        unset($k);unset($v);

        $this->assign('report', $report);

        $html = array();
        $html['report_title'] = $this->view->fetch('Car/report_title');
        $html['report_base'] = $this->view->fetch('Car/report_base');
        $html['report_procedure'] = $this->view->fetch('Car/report_procedure');
        $html['report_config'] = $this->view->fetch('Car/report_config');
        $html['report_perform'] = $this->view->fetch('Car/report_perform');
        $this->view->assign('big_pic_json', $this->get_base_pics_json());
        $this->view->assign('small_pic_json', $this->get_base_pics_json('1'));
        $html['report_base_pics'] = $this->view->fetch('Car/report_base_pics');
        $this->view->assign('appear11', $this->get_pos_class()); //11个覆盖件历史修复情况
        $this->view->assign('frame7', $this->get_pos_class('frame')); //7个骨架历史修复情况
        $arr = $this->get_dm_point_class();
        $this->view->assign('frame24', $arr['frame']); //24处损伤位置，不包括自定义位置
        $this->view->assign('outside41', $arr['outside']); //41处损伤位置，不包括自定义位置
        $this->view->assign('inside29', $arr['inside']); //29处操作位置，不包括自定义位置

        $html['report_history'] = $this->view->fetch('Car/report_history');
        $html['report_damage'] = $this->view->fetch('Car/report_damage');
        $html['report_stat'] = $this->view->fetch('Car/report_stat');
        $html['report_damage_pics'] = $this->view->fetch('Car/report_damage_pics');
        return $html;
    }

    /**
     * 报告标题
     * @return multitype:NULL string unknown Ambigous <>
     */
    public function title() {
        $Car_title = array();
        $Car_title['car_no'] = $this->report['car_base']['car_no'];
        $Car_title['check_date'] = date('Y-m-d', strtotime($this->report['owner']['check_date']));
        $Car_title['composite_state'] = $this->report['car_base']['composite_state'];
        $Car_title['acc_level'] = $this->report['car_base']['accident_level'];
        $Car_title['composite_state_desc'] = C('COMPOSITE_STATE_DESC.' . $this->report['car_base']['composite_state']);
        $Car_title['acc_level_desc'] = C('ACC_LEVEL_DESC.' . $this->report['car_base']['accident_level']);
        $arr = explode(',', $this->report['car_base']['history_state']);
        $Car_title['history_state'] = C('HISTORY_STATE.' . $arr[0]);
        if ($arr[1]) {
            $Car_title['history_state'] .= "," . C('HISTORY_STATE.' . $arr[1]);
        };
        //1
        //$arr[1]//2
        $a = new \AuctionCore\Other\Address();
        $area = $a->get_province($this->report['car_base']['reg_city']);
        $Car_title['province'] = $area['province'];
        $Car_title['city'] = $area['city_name'];
        $cm = new \AuctionCore\CarResource\CarModel();

        $brand = $cm->get_model_by_id($this->report['car_base']['brand_id'], 'brand');
        $series = $cm->get_model_by_id($this->report['car_base']['series_id'], 'series');
        $model = $cm->get_model_by_id($this->report['car_base']['model_id']);
        $Car_title['brand'] = $brand['name'];
        $Car_title['series'] = $series['name'];
        $Car_title['car_cc'] = $this->report['car_base']['car_cc'];
        $Car_title['turbo'] = $this->report['car_base']['turbo'];
        $Car_title['gearbox'] = $this->report['car_base']['gearbox'];
        $Car_title['model_show'] = $Car_title['series'] . ' ' . $Car_title['car_cc'] . ($Car_title['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $Car_title['gearbox']);
        $Car_title['model'] = $model['name'];
        //$emp_info = \AuctionCore\Account\Back\Employee::user_info_id($this->report['owner']['checker_id']);
        $emp_info = M('employee')->find($this->report['owner']['checker_id']);
        $Car_title['checker'] = $emp_info['real_name'];
        $Car_title['checker_level'] = C('CHECKER_LEVEL.' . $emp_info['checker_level']);

        return $Car_title;
    }

    /**
     * 车辆基本信息
     * @return multitype
     */
    public function base_info() {
        $manufacturer = M('car_manufacturer')->alias('cm')->join(C('DB_PREFIX') . "car_brand cb ON(cb.manufacturer_id=cm.manufacturer_id)")
            ->where("cb.brand_id='" . $this->report['car_base']['brand_id'] . "'")->getField('cm.full_name');
        $city = new \AuctionCore\Other\Address();
        $reg_city = $city->get_name_by_code($this->report['car_base']['reg_city']);
        $reg_area = $city->get_name_by_code($this->report['car_base']['reg_area']);
        return array(
            'first_reg_date' => array('label' => '初登日期', 'value' => $this->report['car_base']['first_reg_date']),
            'use_properties' => array('label' => '使用性质', 'value' => C('USE_PROPERTIES.' . $this->report['car_base']['use_properties'])),
            'pointer_mileage' => array('label' => '表显里程(万公里)', 'value' => round($this->report['car_base']['pointer_mileage'] / 10000, 1)),
            'ex_factory_date' => array('label' => '出厂日期', 'value' => $this->report['car_base']['ex_factory_date']),
            'reg_city' => array('label' => '注册地', 'value' => $reg_city." ".$reg_area),
            'emission' => array('label' => '排放标准', 'value' => C('EMISSION.' . $this->report['car_base']['emission'])),
            'manufacturer' => array('label' => '汽车厂商', 'value' => $manufacturer),
            'car_cc' => array('label' => '排量', 'value' => $this->report['car_base']['car_cc'] . ($this->report['car_base']['turbo'] ? 'T' : '')),
            'gearbox' => array('label' => '变速箱形式', 'value' => C('GEARBOX.' . $this->report['car_base']['gearbox'])),
            'car_type' => array('label' => '车辆类型', 'value' => C('car_TYPE.' . $this->report['car_base']['car_type'])),
            'drive_method' => array('label' => '驱动方式', 'value' => C('DRIVE_METHOD.' . $this->report['car_base']['drive_method'])),
            'fuel_type' => array('label' => '燃油类型', 'value' => C('FUEL_TYPE.' . $this->report['car_base']['fuel_type'])),
            'car_color' => array('label' => '颜色', 'value' => C('car_COLOR.' . $this->report['car_base']['car_color'])),
            'carrying_num' => array('label' => '核定载客数', 'value' => $this->report['car_base']['carrying_num'] . '人'),
            'tyre_type' => array('label' => '轮胎规格', 'value' => $this->report['car_base']['tyre_type']),
            'brand_model' => array('label' => '品牌型号', 'value' => $this->report['car_base']['brand_model']),
            'engine_no' => array('label' => '发动机号', 'value' => $this->report['car_base']['engine_no']),
            'transfer_times' => array('label' => '过户次数', 'value' => $this->report['car_base']['transfer_times']),
            'cur_use_type' => array('label' => '现使用方', 'value' => C('CUR_USE_TYPE.' . $this->report['car_base']['cur_use_type'])),
            'get_method' => array('label' => '获得方式', 'value' => C('GET_METHOD.' . $this->report['car_base']['get_method'])),
            'is_imported' => array('label' => '是否进口', 'value' => C('IS_IMPORTED.' . $this->report['car_base']['is_imported'])),
        );
    }

    /**
     * 车辆手续信息
     * @return multitype
     */
    public function procedure() {
        $com_insure_city = M('addr_area')->where("city_code='" . $this->report['procedure']['com_insure_city'] . "'")->getField('city_name');
        $arr = explode(',', $this->report['procedure']['com_insure_expiry_remark']);
        if (in_array('2', $arr)) {
            $flag = 1;
        }
        $procedure_info = array(
            'is_reg_cert' => array('label' => '登记证', 'value' => ($this->report['procedure']['is_reg_cert'] == '1' ? '有' : '无')),
            'is_travel_license' => array('label' => '行驶证', 'value' => ($this->report['procedure']['is_travel_license'] == '1' ? '有' : '无')),
            'verify_expiry' => array('label' => '年检有效期', 'value' => date('Y-m', strtotime($this->report['procedure']['verify_expiry']))),
            'invoice' => array('label' => '发票', 'value' => ($this->report['procedure']['invoice'] > 0 ? '有' : '无')),
            'com_insure_expiry' => array('label' => '交强险有效期', 'value' => $flag ? '未见' : $this->report['procedure']['com_insure_expiry']),
            'com_insure_city' => array('label' => '交强险所在地', 'value' => $flag ? '未见' : $com_insure_city),
            'Car_tax_expiry' => array('label' => '车船税', 'value' => $flag ? '未见' : ($this->report['procedure']['Car_tax_expiry'] ? '有(' . date('Y', strtotime($this->report['procedure']['Car_tax_expiry'])) . ')' : '无')),
            'pur_tax_cert' => array('label' => '购置税证', 'value' => ($this->report['procedure']['pur_tax_cert'] == 1 ? '有（征税）' : ($this->report['procedure']['pur_tax_cert'] == 3 ? '无' : '有（免税）'))),
            'maintain_man' => array('label' => '保养手册', 'value' => C('MAINTAIN_MAN.' . $this->report['procedure']['maintain_man'])),
            'nameplate' => array('label' => '车身铭牌', 'value' => ($this->report['procedure']['nameplate'] ? '有' : '无')),
            'ill_deduc_point' => array('label' => '违章记录', 'value' => (($this->report['procedure']['ill_deduc_point'] > 0 || $this->report['procedure']['forfeit'] > 0) ? '有' : '无')),
            'appear_change' => array('label' => '外观改饰', 'value' => ($this->report['procedure']['appear_change'] == '2' ? '实车与行驶证照片不相符' : '无')), //进口车展示此项
            'commodity_inspection_list' => array('label' => '商检单', 'value' => ($this->report['procedure']['commodity_inspection_list'] ? '有' : '无')),
            'import_customs_list' => array('label' => '关单', 'value' => $this->report['procedure']['import_customs_list'] == '1' ? '有' : '无'),
            'bak_key' => array('label' => '备用钥匙', 'value' => ($this->report['procedure']['bak_key'] == '1' ? '有' : '无')),
        );

        if ($this->report['car_base']['is_imported'] == 1) {//进口车不展示此项
            unset($procedure_info['nameplate']);
        }
        return $procedure_info;
    }

    /**
     * 车辆配置信息
     * @return multitype:multitype:string  multitype:string mixed
     */
    public function config() {
        return array(
            'abs' => array('label' => 'ABS', 'value' => C('ABS.' . $this->report['config']['abs'])),
            'air_sac' => array('label' => '气囊', 'value' => $this->report['config']['air_sac'] . '个'),
            'turn_helper' => array('label' => '转向助力', 'value' => C('TURN_HELPER.' . $this->report['config']['turn_helper'])),
            'ele_Car_window' => array('label' => '电动车窗', 'value' => C('ELE_Car_WINDOW.' . $this->report['config']['ele_Car_window'])),
            'air_window' => array('label' => '天窗', 'value' => C('AIR_WINDOW.' . $this->report['config']['air_window'])),
            'mirror' => array('label' => '车外后视镜', 'value' => C('MIRROR.' . $this->report['config']['mirror'])),
            'seat_texture' => array('label' => '座椅材质', 'value' => C('SEAT_TEXTURE.' . $this->report['config']['seat_texture'])),
            'seat_adjust' => array('label' => '座椅调节', 'value' => C('SEAT_ADJUST.' . $this->report['config']['seat_adjust'])),
            'seat_func' => array('label' => '座椅功能', 'value' => C('SEAT_FUNC.' . $this->report['config']['seat_func'])),
            'air_cond' => array('label' => '空调', 'value' => C('AIR_COND.' . $this->report['config']['air_cond'])),
            'audio' => array('label' => '音响设备', 'value' => C('AUDIO.' . $this->report['config']['audio'])),
            'dvd' => array('label' => 'DVD', 'value' => C('DVD.' . $this->report['config']['dvd'])),
            'navi' => array('label' => '导航', 'value' => C('NAVI.' . $this->report['config']['navi'])),
            'fixed_cruise' => array('label' => '定速巡航', 'value' => C('FIXED_CRUISE.' . $this->report['config']['fixed_cruise'])),
            'reverse_radar' => array('label' => '倒车雷达', 'value' => C('REVERSE_RADAR.' . $this->report['config']['reverse_radar'])),
            'tire_hub_texture' => array('label' => '轮毂材料', 'value' => C('TIRE_HUB_TEXTURE.' . $this->report['config']['tire_hub_texture'])),
            'inte_key' => array('label' => '智能钥匙', 'value' => C('INTE_KEY.' . $this->report['config']['inte_key'])),
            'reverse_video' => array('label' => '倒车影像', 'value' => C('REVERSE_VIDEO.' . $this->report['config']['reverse_video'])),
        );
    }

    /**
     * 车辆基本照片-小图
     */
    public function base_pics($flag=false) {
        $base_pics = $this->report['pictures'];
        $arr = array();
        foreach ($base_pics as $k => $v) {
            if (in_array($v['pic_key'], array('p_vin', 'p_travel_license', 'p_nameplate'))) {//报告不展示 车驾号，车辆铭牌，行驶证
                continue;
            }
            if($flag){
                $v['pic_url'] = $v['pic_url'];
            }else{
                $v['pic_url'] = $v['pic_url'].'@285w_190h_4e';
            }
            $arr[$k] = $v;
            if ($k >= 11) {
                break;
            }
        }
        return $arr;
    }

    /**
     * 车辆基本照片-大图
     * @parm string $i
     * @return string
     */
    public function get_base_pics_json($i = '') {
        $base_pics = $this->report['pictures'];
        $arr = array();
        foreach ($base_pics as $k => $v) {
            if (in_array($v['pic_key'], array('p_vin', 'p_travel_license', 'p_nameplate'))) {//报告不展示 车驾号，车辆铭牌，行驶证
                unset($base_pics[$k]);
            } else {
                $arr['pic' . $i][$k]['img'] = $v['pic_url'].'@950w_630h_4e';
                $arr['pic' . $i][$k]['text'] = $v['pic_desc'];
            }
        }
        return json_encode($arr);
    }

    /**
     * 车辆损伤照片
     */
    public function damage_pics(){
        $detail = $this->detail();
        $damage_pics = json_decode($detail['damage_pics'],true);
        return $damage_pics;
    }

    //工况
    public function perform() {
        return array(
            'starter' => array('label' => '起动机', 'value' => $this->get_the_word('STARTER', $this->report['perform']['starter'])),
            'steering_engine' => array('label' => '转向机', 'value' => $this->get_the_word('STEERING_ENGINE', $this->report['perform']['steering_engine'])), //多选
            'engine' => array('label' => '发动机', 'value' => $this->get_the_word('ENGINE', $this->report['perform']['engine'])), //多选
            'ele_sys' => array('label' => '电器故障', 'value' => $this->get_the_word('ELE_SYS', $this->report['perform']['ele_sys'])), //多选
            'engine_box' => array('label' => '变速箱', 'value' => $this->get_the_word('GEAR_BOX_DAMAGE', $this->report['perform']['engine_box'])), //多选
            'bak_tire' => array('label' => '备胎状态', 'value' => ($this->report['perform']['bak_tire'] == '1' ? '有' : '无')), //单选
            'tool_state' => array('label' => '工具状态', 'value' => $this->get_the_word('TOOL_STATE', $this->report['perform']['tool_state']) . ($this->report['perform']['tool_state'] == '4' ? '(三角架，灭火器)' : '')), //多选
            'other_info' => array('label' => '车辆补充说明', 'value' => $this->report['perform']['other_info']), //单选
        );
    }

    public function appear_history() {
        return array(
            'l_f_door' => array('label' => '左前门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_f_door'])),
            'r_f_door' => array('label' => '右前门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_f_door'])),
            'l_b_door' => array('label' => '左后门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_door'])),
            'r_b_door' => array('label' => '右后门', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_door'])),
            'l_f_wing_board' => array('label' => '左前翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_f_wing_board'])),
            'r_f_wing_board' => array('label' => '右前翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_f_wing_board'])),
            'l_b_wing_board' => array('label' => '左后翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_wing_board'])),
            'r_b_wing_board' => array('label' => '右后翼子板', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_wing_board'])),
            'engine_cover' => array('label' => '发动机舱盖', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['engine_cover'])),
            'car_top' => array('label' => '车顶', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['car_top'])),
            'trunk_cover' => array('label' => '后备箱盖', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['trunk_cover'])),
        );
    }

    public function frame_history() {
        return array(
            'l_a_bar' => array('label' => '左A柱', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_a_bar'])),
            'l_b_bar' => array('label' => '左B柱', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_b_bar'])),
            'l_c_bar' => array('label' => '左C柱', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['l_c_bar'])),
            'r_a_bar' => array('label' => '右A柱', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_a_bar'])),
            'r_b_bar' => array('label' => '右B柱', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_b_bar'])),
            'r_c_bar' => array('label' => '右C柱', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['r_c_bar'])),
            'cover_inner_edge' => array('label' => '机盖内缘', 'value' => C('HIDDEN_DAMAGE_CONTENT.' . $this->report['repair_history']['cover_inner_edge'])),
        );
    }

    /**
     * 统计损伤图片
     * @param string $column 字段 (visible_type,dm_degree)
     * @return Array
     * */
    public function get_dm_pic_stat($column = 'visible_type') {
        $res = M('car_damage_pics')->alias('cdp')->field("COUNT(*) AS cnt ,cdp." . $column)
           // ->join(C('DB_PREFIX') . "car_damage cd ON cd.dm_id=cdp.dm_id")
            ->where("cdp.Car_id='" . $this->Car_id . "' " . ($column == 'dm_degree' ? 'AND cdp.dm_degree>1' : ''))->group("cdp." . $column)->select();
        $ret = array();
        foreach ($res as $v) {
            $ret[$v[$column]] = $v['cnt'];
        }
        return $ret;
    }

    /**
     * 统计损伤
     * @param string $column 字段
     * @return Array
     */
    public  function get_dm_stat($column = 'visible_type') {
        $res = M('car_damage_pics')->field('COUNT(*) AS cnt,' . $column)->where("car_id='" . $this->Car_id . "'")->group($column)->select();

	    $ret = array();
        foreach ($res as $v) {
            $ret[$v[$column]] = $v['cnt'];
        }
        return $ret;
    }

    /**
     * 获取损伤图片
     * @param int $val 值
     * @param int $limit 返回限制
     * @param int $type 类型
     * @return Array
     */
    public  function get_dm_pic($val = '2', $limit = '4', $type = 'degree') {
        $cond = "cd.car_id='" . $this->Car_id . "'";
        if ($type == 'degree') {
            $cond .= " and cdp.dm_degree='" . $val . "'";
        } elseif ($type == 'visible') {
            $cond .= " and cdp.visible_type='" . $val . "'";
        } else {
            return '';
        }
        $arr = M('car_damage')->alias('cd')->field('cd.dm_id,cd.car_id,cd.position,cd.cus_pos_name,cdp.visible_type,cdp.dm_degree,cdp.describe,cdp.url')->join(C('DB_PREFIX') . "car_damage_pics cdp ON(cd.dm_id=cdp.dm_id)")
            ->where($cond)->limit(0, $limit)->select();
        foreach($arr as $key => $value){
            $arr[$key]['url'] = get_content_cdnurl().$value['url'];
        }
        return $arr;
    }

    /**
     *  获取所有损伤图片(jquery 页面调用 些方法）
     * @return json
     */
    public function get_all_pics($flag=false,$type='light') {
        if(!$flag){
            $type = I('get.type', 'light');
        }
        $visible = $this->get_dm_stat(); //可见损伤统计
        $degree = $this->get_dm_stat('dm_degree'); //损伤程度统计
	   // print_r($degree);
        $json = '';

        switch ($type) {
            case 'light':
                $light_all_pics = $this->get_dm_pic('2', $degree['2'] * 10); //所有轻微损伤图片
                $json = json_encode($light_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'normal':
                $normal_all_pics = $this->get_dm_pic('3', $degree['3'] * 10); //所有一般损伤图片
                $json = json_encode($normal_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'important':
                $important_all_pics = $this->get_dm_pic('4', $degree['4'] * 10); //所有重要损伤图片
                $json = json_encode($important_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'frame':
                $frame_all_pics = $this->get_dm_pic('3', $visible['3'] * 10, 'visible'); //所有骨架损伤图片
                $json = json_encode($frame_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'outside':
                $outside_all_pics = $this->get_dm_pic('2', $visible['2'] * 10, 'visible'); //所有外饰损伤图片
                $json = json_encode($outside_all_pics,JSON_UNESCAPED_UNICODE);
                break;
            case 'inside':
                $inside_all_pics = $this->get_dm_pic('1', $visible['1'] * 10, 'visible'); //所有内饰损伤图片
                $json = json_encode($inside_all_pics,JSON_UNESCAPED_UNICODE);
                break;
        }
        if($flag){
            return $json;
        }else{
            echo $json;
        }
        exit;
    }

    /**
     * 格式化输出损伤图片(接口)
     */
    public function dm_pic_to_json() {
        $visible = $this->get_dm_stat(); //可见损伤统计
        $degree = $this->get_dm_stat('dm_degree'); //损伤程度统计
        //所有轻微损伤图片
        $arr = array();
        $light_all_pics = $this->get_dm_pic('2', $degree['2'] * 10);
        $arr['slight'] = array();
        if ($light_all_pics) {
            foreach ($light_all_pics as $k => $v) {
                $arr['slight'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['slight'][$k]['img'] = $v['url'].'@950w_630h_4e';
            }
        }
        //所有一般损伤图片
        $normal_all_pics = $this->get_dm_pic('3', $degree['3'] * 10);
        $arr['general'] = array();
        if ($normal_all_pics) {
            foreach ($normal_all_pics as $k => $v) {
                $arr['general'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['general'][$k]['img'] = $v['url'].'@950w_630h_4e';
            }
        }
        //所有重要损伤图片
        $important_all_pics = $this->get_dm_pic('4', $degree['4'] * 10);
        $arr['serious'] = array();
        if ($important_all_pics) {
            foreach ($important_all_pics as $k => $v) {
                $arr['serious'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['serious'][$k]['img'] = $v['url'].'@950w_630h_4e';
            }
        }


        $frame_all_pics = $this->get_dm_pic('3', $visible['3'] * 10, 'visible'); //所有骨架损伤图片
        $arr['skeleton'] = array();
        if ($frame_all_pics) {
            foreach ($frame_all_pics as $k => $v) {
                $arr['skeleton'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['skeleton'][$k]['img'] = $v['url'].'@950w_630h_4e';
            }
        }
        $outside_all_pics = $this->get_dm_pic('2', $visible['2'] * 10, 'visible'); //所有外饰损伤图片
        $arr['appearance'] = array();
        if ($outside_all_pics) {
            foreach ($outside_all_pics as $k => $v) {
                $arr['appearance'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['appearance'][$k]['img'] = $v['url'].'@950w_630h_4e';
            }
        }

        $inside_all_pics = $this->get_dm_pic('1', $visible['1'] * 10, 'visible'); //所有内饰损伤图片
        $arr['interior'] = array();
        if ($inside_all_pics) {
            foreach ($inside_all_pics as $k => $v) {
                $arr['interior'][$k]['text'] = $v['cus_pos_name'] . ' ' . $v['describe'];
                $arr['interior'][$k]['img'] = $v['url'].'@950w_630h_4e';
            }
        }
        echo json_encode($arr);
        exit;
    }

    /**
     * 转换以逗号分隔的数字为相应的配置文件中的汉字说明
     * @param $data_src string 数据源 (例：$this->report['perform']['starter'])
     * @param $key string 键名（即Car_config.php中的键名 (例：starter（启动机）
     * @return string
     */
    private function get_the_word($key, $data_src) {
        $tmp = explode(',', $data_src);
        $str = '';
        foreach ($tmp as $v) {
            $str .= C(strtoupper($key) . '.' . $v) . '，';
        }
        $str = rtrim($str, '，');
        return $str;
    }

    /**
     * 模板变量赋值
     * @access protected
     * @param mixed $name 要显示的模板变量
     * @param mixed $value 变量的值
     * @return Object
     */
    protected function assign($name, $value = '') {
        $this->view->assign($name, $value);
        return $this;
    }

    /**
     * 获取可见损伤点样式
     * @param string $type (frame,outside,inside)
     */
    public function get_dm_point_class() {
        $pos = array();
        $res = $this->Car->get_dm_pos('car_damage');
        foreach ($res as $k => $v) {
            if ($v['position'] >= 1 and $v['position'] <= 20) {//内饰
                $i = $v['position']; //2 轻微损伤 3 一般损伤 4 重要损伤
                $pos['inside'][$v['position']] = ($v['dm_degree'] == 2 ? $i . '_' . $i . '_' . $i : ($v['dm_degree'] == 3 ? $i . '_' . $i : ($v['dm_degree'] == 4 ? $i : '')));
            } elseif ($v['position'] >= 22 and $v['position'] <= 69) {//外观
                $i = $v['position'] - 21;
                $pos['outside'][$v['position']] = ($v['dm_degree'] == 2 ? $i . '_' . $i . '_' . $i : ($v['dm_degree'] == 3 ? $i . '_' . $i : ($v['dm_degree'] == 4 ? $i : '')));
            } elseif ($v['position'] >= 71 and $v['position'] <= 93) {//骨架
                $i = $v['position'] - 70;
                $pos['frame'][$v['position']] = ($v['dm_degree'] == 2 ? $i . '_' . $i . '_' . $i : ($v['dm_degree'] == 3 ? $i . '_' . $i : ($v['dm_degree'] == 4 ? $i : '')));
            }
            if ($v['position'] == '96') {//骨架，后围板
                $pos['frame'][$v['position']] = ($v['dm_degree'] == 2 ? '24_24_24' : ($v['dm_degree'] == 3 ? '24_24' : ($v['dm_degree'] == 4 ? '24' : '')));
            }
        }
        return $pos;
    }

    /**
     * 11个覆盖件与7个骨架位置
     *  Car/report_history.html
     *  <div class="xiufuBg{$pos}" style="display:block"></div>
     *  例：左前翼子板 重新喷漆 <div class="xiufuBg1" style="display:block"></div>
     *  例：左前翼子板 重新喷漆且有腻子 <div class="xiufuBg_1" style="display:block"></div>
     * @param string $type 类型（appear 外观 ,frame 骨架
     *  @return Array
     */
    public function get_pos_class($type = 'appear') {
        $pos = array();
        $res = $this->Car->detail('Car_repair_history');
        //外观覆盖件
        foreach ($res as $k => $v) {
            switch ($k) {
                case 'l_f_wing_board':
                    $pos[$k] = ($v == 2 ? '1' : ($v == 3 ? '_1' : ''));
                    break;
                case 'l_f_door':
                    $pos[$k] = ($v == 2 ? '2' : ($v == 3 ? '_2' : ''));
                    break;
                case 'l_b_door':
                    $pos[$k] = ($v == 2 ? '4' : ($v == 3 ? '_4' : ''));
                    break;
                case 'l_b_wing_board':
                    $pos[$k] = ($v == 2 ? '3' : ($v == 3 ? '_3' : ''));
                    break;
                case 'trunk_cover':
                    $pos[$k] = ($v == 2 ? '10' : ($v == 3 ? '_10' : ''));
                    break;
                case 'r_b_wing_board':
                    $pos[$k] = ($v == 2 ? '7' : ($v == 3 ? '_7' : ''));
                    break;
                case 'r_b_door':
                    $pos[$k] = ($v == 2 ? '8' : ($v == 3 ? '_8' : ''));
                    break;
                case 'r_f_door':
                    $pos[$k] = ($v == 2 ? '6' : ($v == 3 ? '_6' : ''));
                    break;
                case 'r_f_wing_board':
                    $pos[$k] = ($v == 2 ? '5' : ($v == 3 ? '_5' : ''));
                    break;
                case 'engine_cover':
                    $pos[$k] = ($v == 2 ? '9' : ($v == 3 ? '_9' : ''));
                    break;
                case 'car_top':
                    $pos[$k] = ($v == 2 ? '11' : ($v == 3 ? '_11' : ''));
                    break;
                default:
                    break;
            }
        }
        if ($type == 'appear') {
            return $pos;
        }
        //骨架
        $pos = array();
        foreach ($res as $k => $v) {
            switch ($k) {
                case 'l_a_bar':
                    $pos[$k] = ($v == 2 ? '1' : ($v == 3 ? '_1' : ''));
                    break;
                case 'l_b_bar':
                    $pos[$k] = ($v == 2 ? '2' : ($v == 3 ? '_2' : ''));
                    break;
                case 'l_c_bar':
                    $pos[$k] = ($v == 2 ? '3' : ($v == 3 ? '_3' : ''));
                    break;
                case 'r_c_bar':
                    $pos[$k] = ($v == 2 ? '4' : ($v == 3 ? '_4' : ''));
                    break;
                case 'r_b_bar':
                    $pos[$k] = ($v == 2 ? '5' : ($v == 3 ? '_5' : ''));
                    break;
                case 'r_a_bar':
                    $pos[$k] = ($v == 2 ? '6' : ($v == 3 ? '_6' : ''));
                    break;
                case 'cover_inner_edge':
                    $pos[$k] = ($v == 2 ? '7' : ($v == 3 ? '_7' : ''));
                    break;
                default:
                    break;
            }
        }
        return $pos;
    }

}
